package com.kingsoft.dc.khaos

import com.kingsoft.dc.khaos.metadata.ModuleCommon
import org.apache.spark.serializer.Serializer

/**
  * Created by chengguangqing on 2019/6/2.
  */
class KhaosEnv(val _id: String,
               val _priority: Int,
               val _dittoInfo: List[ModuleCommon],
               val serializer: Serializer,
               val conf: KhaosConf) {

  private[khaos] var isStopped = false

  private[khaos] def stop(): Unit = {
    if (!isStopped) {
      isStopped = true
    }
  }
}
  object KhaosEnv {
    private var env: KhaosEnv = _

    def set(e: KhaosEnv) {
      env = e
    }

    def get: KhaosEnv = {
      env
    }

    def createEnv(applicationId: String,
                  conf: KhaosConf,
                  khaosInfo: List[ModuleCommon]): KhaosEnv = {
      create(applicationId, conf, khaosInfo)
    }

    private def create(applicationId: String,
                       conf: KhaosConf,
                       khaosInfo: List[ModuleCommon]): KhaosEnv = {
      val id = applicationId
      val priority = conf.getInt("khaos.app.priority", 1)
      val serializer = null

      val envInstance = new KhaosEnv(
        id,
        priority,
        khaosInfo,
        serializer,
        conf
      )
      envInstance
    }

    def getThreadLocal: KhaosEnv  = {
      env
    }
}
